import Vue from 'vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import store from './store'
import axios from 'axios'
import VueAxios from 'vue-axios'
import {Location} from 'vue-router';

axios.defaults.baseURL = 'https://localhost:8081';


Vue.config.productionTip = false
Vue.use(ElementUI);
Vue.use(VueAxios, axios)

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')



// 添加请求拦截器，在请求头中加token
axios.interceptors.request.use(
    config => {
        //从localStorage中获取token
        var token = localStorage.getItem('Authorization')
        if (token) {
            //如果localStorage中有token,就把token放入请求头 fds
            // @ts-ignore
            config.headers.Authorization = token
        }
        console.log(config);
        return config;
    },
    error => {
        return Promise.reject(error);
    });

//响应拦截器,接收网关过滤器中传来数据
axios.interceptors.response.use(function (response) {
    // 在接收响应做些什么，例如跳转到登录页
    // @ts-ignore
    if(response.data.code==401){
        localStorage.removeItem('Authorization');
        //跳转到登录页面
        router.push({path:"/common"})
    }
    console.log("+++"+response)
    return response;
}, function (error) {
    console.log("--------"+error)
    // 对响应错误做点什么
    return Promise.reject(error);
});


router.beforeEach((to, from, next) => {
    //导航卫士,在这里进行路由器的拼接
    console.log("我是全局导航守卫");

    //alert(uid);
    //从vuex全局参数里获取用户信息
    let uid = store.state.user.id;
    //根据用户id查询对应的菜单
    //进入主界面的时候
    if (to.path == '/home') {
        axios.get('http://localhost:8081/menu/findMenuByUid', {
            params: {
                uid: uid
            }
        }).then((response) => {
            //获取menu资源
            let menus = response.data;
            console.log(menus);
            //考虑把这个menus资源转换成我们的路由对象
            let menuRouters = setRouter(menus);
            console.log("**********"+JSON.stringify(menuRouters))
            //赋值给路由的home中的children
            // @ts-ignore
            router.options.routes[0].children = menuRouters;
            //加入到路由中
            // @ts-ignore
            router.addRoutes(router.options.routes);
            //跳转到中转页面:next();是不是死循环操作
            next({path: '/common'});
        });
    } else {
        next();
    }

    // @ts-ignore
    function setRouter(menus) {
        let menuRouters = [];
        //先转换一级菜单资源
        for (let i = 0; i < menus.length; i++) {
            let menu = menus[i];

            let children = [];
            //递归处理
            if(menu.children && menu.children instanceof Array){
                // @ts-ignore
                children = setRouter(menu.children);
            }
            //拼接孩子的操作节点
            // @ts-ignore
            let oper = children.map((x)=>x.path).join(",");
            //创建路由对象
            // @ts-ignore
            let newRouter = {
                path: menu.path,
                name: menu.label,
                iconCls: menu.iconCls,
                children: children,
                oper: oper

            }
            //获取组件名称
            let componentName = menu.component;
            //组件
            newRouter.component = () => import('./views/'+componentName+'.vue');
            //放入到menuRouters
            menuRouters.push(newRouter);
        }

        return menuRouters;
    }

})








